<%@ include file="/WEB-INF/template/include.jsp"%>
<%@ include file="localHeader.jsp"%>
<openmrs:htmlInclude file="/scripts/calendar/calendar.js" />
<openmrs:htmlInclude file="/scripts/jquery/jquery-1.3.2.min.js" />
<openmrs:require privilege="View Orders" otherwise="/login.htm" redirect="/module/simplelabentry/resultEntry.htm" />

<br/>
<script type="text/javascript">
$(document).ready(function() {
	// set the default of orderDate
	$("#orderDate").val("${todayStr}");
});
</script>

<form id="resultEntry" action="resultEntry.htm" method="get">
    <b class="boxHeader">Step 1. See Patient List by choosing lab:</b>
    <div class="box">
        <table>

            <tr>
                <td>
                    Date:
                </td>
                <td>
                    <input type="text" id="orderDate" name="orderDate" size="10" value="${param.orderDate}" onFocus="showCalendar(this)" />
                </td>
                <td>
		Enter Patient ID/Name:
                </td>
                <td>
                    <input type="text" id="identifier" name="identifier" class="orderField" value="${param.identifier}" />
                <td>

                <td>
		Select Lab For Patient List:
                </td>
                <td>

                    <select name="labName" id="labName" onChange="submitForm()">
                        <option id="select" value="Select" selected="selected">Select</option>
                        <c:forEach items="${headingList}" var="tabHeading">

                            <openmrs:hasPrivilege privilege="View ${tabHeading.name}">
                                <option id="${tabHeading.name}" name="${tabHeading.name}" value="${tabHeading.name}" <c:if test="${!empty param.labName && param.labName == tabHeading.name}">selected="selected"</c:if>><spring:message code="${tabHeading.name}"/></option>
                            </openmrs:hasPrivilege>
                        </c:forEach>
                    </select>
                </td>
            </tr>
            <tr>
            </tr>
            <tr>
            </tr>
            <tr>
                <td colspan="2"><input type="button" value="Get Patient List" onClick="submitForm()"/></td>
                <td colspan="2"><input type="button" value="Reset" onClick="resetForm()"/></td>
            <c:forEach items="${headingList}" var="tabHeading">
                <c:if test="${!empty param.labName && param.labName == tabHeading.name && param.labName != 'Select'}">
                    <td>
			Filter by Test:
                    </td>
                    <td colspan="4">
                        <select name="groupKey" id="groupKey" onChange="submitForm()">
                            <option id="select" value="Select" selected="selected">Select</option>
                            <c:forEach items="${tabHeading.answers}" var="conceptAns">
                                <option name="${conceptAns.answerConcept.id}" value="${conceptAns.answerConcept.name}" <c:if test="${!empty param.groupKey && param.groupKey == conceptAns.answerConcept.name}">selected="selected"</c:if>><spring:message code="${conceptAns.answerConcept.name}"/></option>
                            </c:forEach>
                        </select>
                    </td>
                </c:if>
            </c:forEach>

            </tr>

        </table>
    </div>
</form>

<br/>

<c:if test="${!empty param.groupKey || !empty param.labName}">
    <c:choose>
        <c:when test="${!empty param.identifier}">
            <openmrs:portlet url="orderEntry"
                             id="orderEntrySectionId"
                             moduleId="simplelabentry"
                             parameters="workList=false|confidentialTest=false|limit=open|identifier=${param.identifier}|groupKey=${param.groupKey}" />
        </c:when>
        <c:otherwise>
            <openmrs:portlet
                url="orderEntry"
                id="orderEntrySectionId"
                moduleId="simplelabentry"
                parameters="workList=false|confidentialTest=false|limit=open|allowCategoryEdit=false|groupKey=${param.groupKey}|orderDate=${param.orderDate}|orderLocation=1|identifier=${param.identifier}" />
        </c:otherwise>
    </c:choose>
</c:if>


<script type="text/javascript">
    function resetForm(){
        window.location.href = "resultEntry.htm";
    }
    function submitForm(){
        var orderDate = document.getElementById('orderDate').value;
        var identifier = document.getElementById('identifier').value;
        var error = false;
        if(orderDate!=null && orderDate!="")
        {
            if(!dateISO(orderDate))
            {
                alert("Please enter valid Order Date");
                document.getElementById('orderDate').value = "";
                error = true;
            }
        }

        if(identifier!=null && identifier!="")
        {
            if(!lettersonly(identifier))
            {
                alert("Please enter valid identifier");
                document.getElementById('identifier').value = "";
                error = true;
            }
        }
        if(error==false)
        {
            document.getElementById('resultEntry').submit();
        }


    }

    function lettersonly(value) {
        return /^[a-z0-9\-\s]+$/i.test(value);
    }

    function dateISO(value) {
        return /^\d{2}[\/-]\d{2}[\/-]\d{4}$/i.test(value);
    }

</script>
<%@ include file="/WEB-INF/template/footer.jsp"%>
